package com.hele.sts.action.admin;

import java.util.List;
import java.util.Map;

import com.hele.sts.dao.User;
import com.hele.sts.dao.XtLogDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hele.sts.dao.XtServer;
import com.hele.sts.dao.XtServerDao;
import com.hele.sts.webwork.BaseAction;
import com.opensymphony.xwork2.ActionContext;
import org.springframework.beans.factory.annotation.Autowired;

public class ServerManagerAction extends BaseAction {

	@Autowired
	private XtLogDao xtLogDao;

	@Override
    public String execute() throws Exception {
		ActionContext ctx = ActionContext.getContext();
		Map session = ctx.getSession();
		String loginName = (String) session.get("AdminUser.loginName");
		if ((loginName == null) || (loginName.length() < 1)) {
			setErrorInfo("没有登录！");
			return "error";
		}
		return super.execute();
	}

	@Override
    public String update() {
		ActionContext ctx = ActionContext.getContext();
		Map session = ctx.getSession();
		User adminUser = (User)session.get(("AdminUser"));

		XtServer bean = XtServerDao.load(this.djjg, this.strategy, this.serverXh);
		if (bean == null) {
			setErrorInfo("找不到该服务器！");
			return "error";
		}
		if ((this.webAddress != null) && (this.webAddress.length() > 0)) {
			bean.setWebAddress(this.webAddress);
		}
		if ((this.ftpUser != null) && (this.ftpUser.length() > 0)) {
			bean.setFtpUser(this.ftpUser);
		}
		if ((this.ftpPassword != null) && (this.ftpPassword.length() > 0)) {
			bean.setFtpPassword(this.ftpPassword);
		}
		if ((this.serverName != null) && (this.serverName.length() > 0)) {
			bean.setServerName(this.serverName);
		}
		if ((this.diskName != null) && (this.diskName.length() > 0)) {
			bean.setDiskName(this.diskName);
		}
		if ((this.ipAddress != null) && (this.ipAddress.length() > 0)) {
			bean.setIpAddress(this.ipAddress);
		}
		if ((this.serverDisk != null) && (this.serverDisk.length() > 0)) {
			bean.setServerDisk(this.serverDisk);
		}
		if ((this.errCheck != null) && (this.errCheck.length() > 0)) {
			bean.setErrCheck(this.errCheck);
		}
		if ((this.pageCheck != null) && (this.pageCheck.length() > 0)) {
			bean.setPageCheck(this.pageCheck);
		}
		if ((this.djjgShow != null) && (this.djjgShow.length() > 0)) {
			bean.setDjjgShow(this.djjgShow);
		}
		boolean b = true;
		b = XtServerDao.update(bean);
		if (!b) {
			xtLogDao.insertLog("业务日志","后管服务器修改失败",adminUser.getUserId(),adminUser.getUserName(),"成功","用户："+adminUser.getUserId()+" 修改服务器"+this.djjg,this.getClass().getName());

			setErrorInfo("服务器修改失败！");
			return "error";
		}
		xtLogDao.insertLog("业务日志","后管服务器修改成功",adminUser.getUserId(),adminUser.getUserName(),"成功","用户："+adminUser.getUserId()+" 修改服务器"+this.djjg,this.getClass().getName());

		return "update";
	}

	@Override
    public String view() {
		this.xtServer = XtServerDao.load(this.djjg, this.strategy, this.serverXh);
		if (this.xtServer == null) {
			setErrorInfo("找不到服务器号！");
			return "error";
		}
		return "view";
	}

	@Override
    public String edit() {
        ActionContext ctx = ActionContext.getContext();
        Map session = ctx.getSession();
        User adminUser = (User)session.get(("AdminUser"));
		String info = " 注意：服务器序号，登记机关和策略不能同时与另一个服务器相同";
		if (!doCheckInput()) {
			return "error";
		}
		boolean b = true;
		b = XtServerDao.insert(createXtServer());
		if (!b) {
            xtLogDao.insertLog("业务日志","后管服务器添加失败",adminUser.getUserId(),adminUser.getUserName(),"成功","用户："+adminUser.getUserId()+" 修改服务器"+this.djjg,this.getClass().getName());

            setErrorInfo("添加服务器失败！请检查是否已有该服务器!" + info);
			return "error";
		}
        xtLogDao.insertLog("业务日志","后管服务器添加成功",adminUser.getUserId(),adminUser.getUserName(),"成功","用户："+adminUser.getUserId()+" 添加服务器"+this.djjg,this.getClass().getName());

        return "edit";
	}

	@Override
    public String delete() {
        ActionContext ctx = ActionContext.getContext();
        Map session = ctx.getSession();
        User adminUser = (User)session.get(("AdminUser"));

		boolean b = true;
		b = XtServerDao.delete(this.djjg, this.strategy, this.serverXh);
		if (!b) {
            xtLogDao.insertLog("业务日志","后管服务器删除失败",adminUser.getUserId(),adminUser.getUserName(),"成功","用户："+adminUser.getUserId()+" 修改服务器"+this.djjg,this.getClass().getName());

            setErrorInfo("删除服务器失败！");
			return "error";
		}
        xtLogDao.insertLog("业务日志","后管服务器删除成功",adminUser.getUserId(),adminUser.getUserName(),"成功","用户："+adminUser.getUserId()+" 修改服务器"+this.djjg,this.getClass().getName());

        return "del";
	}

	@Override
    public String getHref() {
		String href = "";
		if (this.serverXh != 0L) {
			href = href + "&serverXh=" + this.serverXh;
		}
		if ((this.serverName != null) && (this.serverName.length() > 0)) {
			href = href + "&serverName=" + this.serverName;
		}
		return href;
	}

	@Override
    public int count() {
		return XtServerDao.count(this.serverXh, this.serverName);
	}

	@Override
    public String list() {
		ActionContext ctx = ActionContext.getContext();
		Map session = ctx.getSession();
        User adminUser = (User)session.get(("AdminUser"));
		String djjg = (String) session.get("AdminUser.djjg");
		System.out.println(djjg);

		this.xtServerList = XtServerDao.load(this.serverXh, this.serverName, getPageNo(), getPageSize(), djjg);
        xtLogDao.insertLog("业务日志","后管服务器列表查询成功",adminUser.getUserId(),adminUser.getUserName(),"成功","用户："+adminUser.getUserId()+" 服务器列表查询",this.getClass().getName());

        return "list";
	}

	boolean doCheckInput() {
		boolean flag = true;
		if ((this.djjg == null) || (this.djjg.length() < 1)) {
			setErrorInfo("登记机关不能为空");
			flag = false;
		}
		return flag;
	}

	XtServer createXtServer() {
		XtServer xtServer = new XtServer();
		xtServer.setStrategy(this.strategy);
		xtServer.setDiskName(this.diskName);
		xtServer.setIpAddress(this.ipAddress);
		xtServer.setServerDisk(this.serverDisk);
		xtServer.setServerName(this.serverName);
		xtServer.setServerXh(this.serverXh);
		xtServer.setDjjg(this.djjg);
		xtServer.setWebAddress(this.webAddress);
		xtServer.setFtpUser(this.ftpUser);
		xtServer.setFtpPassword(this.ftpPassword);
		xtServer.setErrCheck(errCheck);
		xtServer.setPageCheck(pageCheck);
		xtServer.setDjjgShow(djjgShow);
		return xtServer;
	}

	public String getErrorInfo() {
		return this.errorInfo;
	}

	public void setErrorInfo(String errorInfo) {
		this.errorInfo = errorInfo;
	}

	public long getServerXh() {
		return this.serverXh;
	}

	public void setServerXh(long serverXh) {
		this.serverXh = serverXh;
	}

	public String getServerName() {
		return this.serverName;
	}

	public void setServerName(String serverName) {
		this.serverName = serverName;
	}

	public String getDiskName() {
		return this.diskName;
	}

	public void setDiskName(String diskName) {
		this.diskName = diskName;
	}

	public String getIpAddress() {
		return this.ipAddress;
	}

	public void setIpAddress(String ipAddress) {
		this.ipAddress = ipAddress;
	}

	public String getServerDisk() {
		return this.serverDisk;
	}

	public void setServerDisk(String serverDisk) {
		this.serverDisk = serverDisk;
	}

	public List getXtServerList() {
		return this.xtServerList;
	}

	public void setXtServerList(List xtServerList) {
		this.xtServerList = xtServerList;
	}

	public XtServer getXtServer() {
		return this.xtServer;
	}

	public void setXtServer(XtServer xtServer) {
		this.xtServer = xtServer;
	}

	public String getDjjg() {
		return this.djjg;
	}

	public void setDjjg(String djjg) {
		this.djjg = djjg;
	}

	public String getWebAddress() {
		return this.webAddress;
	}

	public void setWebAddress(String webAddress) {
		this.webAddress = webAddress;
	}

	public String getFtpUser() {
		return this.ftpUser;
	}

	public void setFtpUser(String ftpUser) {
		this.ftpUser = ftpUser;
	}

	public String getFtpPassword() {
		return this.ftpPassword;
	}

	public void setFtpPassword(String ftpPassword) {
		this.ftpPassword = ftpPassword;
	}

	public String getStrategy() {
		return this.strategy;
	}

	public void setStrategy(String strategy) {
		this.strategy = strategy;
	}

	static Logger logger = LoggerFactory.getLogger(ServerManagerAction.class);
	long serverXh;
	String serverName;
	String diskName;
	String ipAddress;
	String serverDisk;
	String errorInfo;
	List xtServerList;
	XtServer xtServer;
	String webAddress;
	String ftpUser;
	String ftpPassword;
	String djjg;
	String strategy;
	String errCheck;
	String pageCheck;
	String djjgShow;

	/**
	 * @return the errCheck
	 */
	public String getErrCheck() {
		return errCheck;
	}

	/**
	 * @param errCheck
	 *            the errCheck to set
	 */
	public void setErrCheck(String errCheck) {
		this.errCheck = errCheck;
	}

	/**
	 * @return the pageCheck
	 */
	public String getPageCheck() {
		return pageCheck;
	}

	/**
	 * @param pageCheck the pageCheck to set
	 */
	public void setPageCheck(String pageCheck) {
		this.pageCheck = pageCheck;
	}

	/**
	 * @return the djjgShow
	 */
	public String getDjjgShow() {
		return djjgShow;
	}

	/**
	 * @param djjgShow the djjgShow to set
	 */
	public void setDjjgShow(String djjgShow) {
		this.djjgShow = djjgShow;
	}

}
